home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 10 / UNARY.CPP < prev   
C/C++ Source or Header  |  1995-02-23  |  5KB  |  195 lines

  1. // File from page 401 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: UNARY.CPP -- Overloading unary operators
  34. #include <iostream.h>
  35.  
  36. class integer {
  37.   long i;
  38. public:
  39.   integer(long I = 0) : i(I) {}
  40.   // No side effects takes const& argument:
  41.   friend const integer&
  42.     operator+(const integer& a);
  43.   friend const integer
  44.     operator-(const integer& a);
  45.   friend const integer
  46.     operator~(const integer& a);
  47.   friend integer*
  48.     operator&(integer& a);
  49.   friend int
  50.     operator!(const integer& a);
  51.   // Side effects don't take const& argument:
  52.   // Prefix:
  53.   friend const integer&
  54.     operator++(integer& a);
  55.   // Postfix:
  56.   friend const integer
  57.     operator++(integer& a, int);
  58.   // Prefix:
  59.   friend const integer&
  60.     operator--(integer& a);
  61.   // Postfix:
  62.   friend const integer
  63.     operator--(integer& a, int);
  64. };
  65.  
  66. // Global operators:
  67. const integer& operator+(const integer& a) {
  68.   cout << "+integer\n";
  69.   return a; // Unary + has no effect
  70. }
  71. const integer operator-(const integer& a) {
  72.   cout << "-integer\n";
  73.   return integer(-a.i);
  74. }
  75. const integer operator~(const integer& a) {
  76.   cout << "~integer\n";
  77.   return integer(~a.i);
  78. }
  79. integer* operator&(integer& a) {
  80.   cout << "&integer\n";
  81.   return &a;
  82. }
  83. int operator!(const integer& a) {
  84.   cout << "!integer\n";
  85.   return !a.i;
  86. }
  87. // Prefix; return incremented value
  88. const integer& operator++(integer& a) {
  89.   cout << "++integer\n";
  90.   a.i++;
  91.   return a;
  92. }
  93. // Postfix; return the value before increment:
  94. const integer operator++(integer& a, int) {
  95.   cout << "integer++\n";
  96.   integer r(a.i);
  97.   a.i++;
  98.   return r;
  99. }
  100. // Prefix; return decremented value
  101. const integer& operator--(integer& a) {
  102.   cout << "--integer\n";
  103.   a.i--;
  104.   return a;
  105. }
  106. // Postfix; return the value before decrement:
  107. const integer operator--(integer& a, int) {
  108.   cout << "integer--\n";
  109.   integer r(a.i);
  110.   a.i--;
  111.   return r;
  112. }
  113.  
  114. void f(integer a) {
  115.   +a;
  116.   -a;
  117.   ~a;
  118.   integer* ip = &a;
  119.   !a;
  120.   ++a;
  121.   a++;
  122.   --a;
  123.   a--;
  124. }
  125.  
  126. // Member operators (implicit "this"):
  127. class byte {
  128.   unsigned char b;
  129. public:
  130.   byte(unsigned char B = 0) : b(B) {}
  131.   // No side effects: const member function:
  132.   const byte& operator+() const {
  133.     cout << "+byte\n";
  134.     return *this;
  135.   }
  136.   const byte operator-() const {
  137.     cout << "-byte\n";
  138.     return byte(-b);
  139.   }
  140.   const byte operator~() const {
  141.     cout << "~byte\n";
  142.     return byte(~b);
  143.   }
  144.   byte operator!() const {
  145.     cout << "!byte\n";
  146.     return byte(!b);
  147.   }
  148.   byte* operator&() {
  149.     cout << "&byte\n";
  150.     return this;
  151.   }
  152.   // Side effects: non-const member function:
  153.   const byte& operator++() { // Prefix
  154.     cout << "++byte\n";
  155.     b++;
  156.     return *this;
  157.   }
  158.   const byte operator++(int) { // Postfix
  159.     cout << "byte++\n";
  160.     byte(b);
  161.     b++;
  162.     return b;
  163.   }
  164.   const byte& operator--() { // Prefix
  165.     cout << "--byte\n";
  166.     --b;
  167.     return *this;
  168.   }
  169.   const byte operator--(int) { // Postfix
  170.     cout << "byte--\n";
  171.     byte(b);
  172.     --b;
  173.     return b;
  174.   }
  175. };
  176.  
  177. void g(byte b) {
  178.   +b;
  179.   -b;
  180.   ~b;
  181.   byte* bp = &b;
  182.   !b;
  183.   ++b;
  184.   b++;
  185.   --b;
  186.   b--;
  187. }
  188.  
  189. main() {
  190.   integer a;
  191.   f(a);
  192.   byte b;
  193.   g(b);
  194. }
  195.